WHILE...WEND Statement Action Executes a series of statements in a loop, as long as a given condition is true. Syntax WHILE condition . . . WEND Remarks The argument condition is a numeric expression that BASIC evaluates as true (nonzero) or false (zero). If condition is true (that is, if it does not equal zero), then any intervening statements are executed until the WEND statement is encountered. BASIC then returns to the WHILE statement and checks condition. If it is still true, the process is repeated. If it is not true (or if it equals zero), execution resumes with the statement following the WEND statement. Note BASIC's DO... LOOP statement provides a more powerful and flexible loop-control structure. WHILE... WEND loops can be nested to any level. Each WEND matches the most recent WHILE. When BASIC encounters an unmatched WHILE statement, it generates the error message WHILE without WEND. If BASIC encounters an unmatched WEND statement, it generates the error message WEND without WHILE. Note Do not branch into the body of a WHILE... WEND loop without executing the WHILE statement. This may cause run-time errors or program problems that are difficult to locate. See Also DO... LOOP Example The following example performs a bubble sort on the array A$. Assigning the variable Exchange a non-zero value (true) forces one pass through the WHILE... WEND loop (this construction is unnecessary with DO... LOOP). When there are no more swaps, all elements of A$ are sorted, Exchange is false (equal to zero), and the program continues execution with the line following the WEND statement. ' Bubble sort of array A$. CONST FALSE = 0, TRUE = -1 DIM I AS INTEGER DIM A$(4) A$(1) = "New York" A$(2) = "Boston" A$(3) = "Chicago" A$(4) = "Seattle" Max = UBOUND(A$) Exchange = TRUE ' Force first pass through the array. WHILE Exchange ' Sort until no elements are exchanged. Exchange = FALSE ' Compare the array elements by pairs. When two are exchanged, ' force another pass by setting Exchange to TRUE. FOR I = 2 TO Max IF A$(I - 1) > A$(I) THEN Exchange = TRUE SWAP A$(I - 1), A$(I) END IF NEXT WEND CLS FOR I = 1 TO 4 PRINT A$(I) NEXT I END